<template>
  <div>
    <el-dialog title="角色" :visible="showDialog">
      <el-checkbox-group v-model="role">
        <el-checkbox
          v-for="item in rolelist"
          :key="item.id"
          :label="item.id"
        >{{ item.name }}</el-checkbox></el-checkbox-group>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button @click="btnok">确定</el-button>
          <el-button @click="btnon">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      rolelist: [],
      role: []
    }
  },

  created() {
    this.getRoleList()
  },

  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.rolelist = rows
      console.log(this.rolelist)
    },
    async getUserDetailById(id) {
      const res = await getUserDetailById(id)
      const { roleIds } = res
      this.role = roleIds
    },
    async  btnok() {
      await assignRoles({
        id: this.userid,
        roleIds: this.role
      })
      this.$message('修改成功')
      this.$emit('update:show-dialog', false)
    },
    btnon() {
      this.$emit('update:show-dialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
